package com.vito.oa.act.extend.action;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.config.annotation.Reference;
import com.vito.act.model.ActExProcessBranchModel;
import com.vito.act.model.ActSubProcessModel;
import com.vito.base.authoriza.action.BaseAction;
import com.vito.base.authoriza.cache.SessionMap;
import com.vito.base.authoriza.model.UserModel;
import com.vito.base.model.DataGrid;
import com.vito.base.model.MyHashMap;
import com.vito.base.model.ResMsg;
import com.vito.base.model.Result;
import com.vito.base.util.Contants;
import com.vito.base.util.StringUtil;
import com.vito.base.util.Validata;
import com.vito.oa.act.extend.model.OaActEExBussDefModel;
import com.vito.oa.act.extend.model.OaActExUserDefinedPro;
import com.vito.oa.act.extend.service.OaActExProcessBranchProService;
/**
 *设置流程分支action类
 * @author tianjing
 *
 */
@Controller
@RequestMapping("oa/act/actExProcessBranch")
public class OaActExProcessBranchAction extends BaseAction {
	
	private static final Logger log = LoggerFactory.getLogger(OaActExProcessBranchAction.class);
	
	@Reference
	private OaActExProcessBranchProService processBranchProService;
	
	/**
	 * 查询已设置的流程分支
	 * @return
	 */
	@RequestMapping("getByPaged")
	@ResponseBody
	public Result queryByPage(ActExProcessBranchModel processBranchModel) {
		Result res = new Result();
		try{
			//获取当前登录用户
			UserModel user = (UserModel) SessionMap.getObj();
			if(null == user){
				res.setCode(Result.CODE_ERROR);
				res.setMsg(ResMsg.MSG_LOGIN_NEED);
				return res;
			}
			if(StringUtil.isEmpty(user.getDeptId())){
				res.setCode(Result.CODE_ERROR);
				res.setMsg("该用户未分配部门，无权进行此操作！");
				return res;
			}
			
			processBranchModel.setDeptId(user.getDeptParentId());
			processBranchModel.setCreateDept(user.getDeptParentId());
			DataGrid<ActExProcessBranchModel> page = processBranchProService.queryActExProcessBranchByPage(processBranchModel);
			if(page.getTotal()!=0){
				res.setCode(Result.CODE_OK);
				res.setData(page);
			}else{
				res.setCode(Result.CODE_NO_DATA);
				res.setMsg("暂无数据");
			}
		}catch(Exception ex){
			log.error("查询异常",ex);
			res.setCode(Result.CODE_ERROR);
			res.setMsg("查询异常");
		}
		return res ;
	}
	@ResponseBody
	@RequestMapping("getSelectPage")
	public Result querySelectPage(@RequestParam Map<String,String> map){
		Result res = new Result();
		map.put("processNodeId", Contants.FORM_PROCESS_START);
		try {
			res.setData(processBranchProService.querySelectPage(map));
			res.setCode(Result.CODE_OK);
		} catch (Exception e) {
			log.error("查询异常",e);
			res.setCode(Result.CODE_ERROR);
			res.setMsg("查询异常");
		}
		return res;
		
	}
	
	
	@ResponseBody
	@RequestMapping("saveSubProcessSet")
	public Result saveSubProcessSet( ActSubProcessModel aspm){
		Result res = new Result();		
		try{
			UserModel user  = SessionMap.getObj();
			if(null == user){
				res.setCode(Result.CODE_ERROR);
				res.setMsg(ResMsg.MSG_LOGIN_NEED);
				return res;
			}
			//如果是系统自定义流程，默认部门sys
			if(Validata.isNullOrEmpty(aspm.getIsDefined())){
				res.setCode(Result.CODE_ERROR);
				res.setMsg("参数错误！");
				return res;
			}
			aspm.setDept_id(user.getDeptParentId());
			processBranchProService.saveSubProcessSet(aspm);
			res.setCode(Result.CODE_OK);
			res.setMsg(ResMsg.SAVESUC);
		}catch(Exception e){
			log.error("保存异常",e);
			res.setCode(Result.CODE_ERROR);
			res.setMsg(ResMsg.EXECERR);
		}
		return res;
	}
	
	
	@ResponseBody
	@RequestMapping("querySubSet")
	public Result querySubSet( @RequestParam Map<String,String> map){
		Result res = new Result();		
		UserModel user  = SessionMap.getObj();
		try {
			if(null == user){
				res.setCode(Result.CODE_ERROR);
				res.setMsg(ResMsg.MSG_LOGIN_NEED);
				return res;
			}
			//如果是系统自定义流程，默认部门sys		   
			if(Validata.isNullOrEmpty(map.get("isDefined"))){
				res.setCode(Result.CODE_ERROR);
				res.setMsg("参数错误！");
				return res;
			}

			map.put("dept_id", user.getDeptParentId());
			res.setData(processBranchProService.querySubSet(map));
			res.setCode(Result.CODE_OK);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("查询异常",e);
			res.setCode(Result.CODE_ERROR);
			res.setMsg("查询异常");
		} 
		return res;
	}
	
	@ResponseBody
	@RequestMapping("queryActBussSubSetByPage")
	public Result queryActBussSubSetByPage(OaActEExBussDefModel actEExBussDefModel){
		Result res = new Result();		
		UserModel user  = SessionMap.getObj();
		try {
			if(null == user){
				res.setCode(Result.CODE_ERROR);
				res.setMsg(ResMsg.MSG_LOGIN_NEED);
				return res;
			}
			actEExBussDefModel.setDeptId( user.getDeptParentId());
			res.setData(processBranchProService.queryActBussSubSetByPage(actEExBussDefModel));
			res.setCode(Result.CODE_OK);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("查询异常",e);
			res.setCode(Result.CODE_ERROR);
			res.setMsg("查询异常");
		} 
		return res;
	}
	
	@RequestMapping("setDealUsers")
	@ResponseBody
	public Result setDealUsers(@RequestParam Map<String,Object> map){
		Result res = new Result();
		try{
			//获取当前登录用户
			OaActExUserDefinedPro userDefinedPro=new OaActExUserDefinedPro();
			userDefinedPro.setSetUserIds(map.get("setUserIds").toString());
			userDefinedPro.setProcessKey(map.get("processKey").toString());
			userDefinedPro.setNodeId(map.get("nodeId").toString());
			userDefinedPro.setBranchId(map.get("branchId").toString());
			userDefinedPro.setOrderNum((int)Double.parseDouble((String)map.get("orderNum")));
			UserModel user = (UserModel) SessionMap.getObj();
			if(null == user){
				res.setCode(Result.CODE_ERROR);
				res.setMsg(ResMsg.MSG_LOGIN_NEED);
				return res;
			}
			if(StringUtil.isEmpty(user.getDeptId())){
				res.setCode(Result.CODE_ERROR);
				res.setMsg("该用户未分配部门，无权进行此操作！");
				return res;
			}
			userDefinedPro.setDeptId(user.getDeptParentId());
			int count = processBranchProService.setDealUsers(userDefinedPro);
			if(count>0){
				res.setCode(Result.CODE_OK);
				res.setMsg(ResMsg.SAVESUC);
			}else{
				res.setCode(Result.CODE_OK);
				res.setMsg(ResMsg.SAVEERR);
			}
		}catch(Exception ex){
			log.error(ResMsg.EXECERR,ex);
			res.setMsg(ResMsg.EXECERR);
			res.setCode(Result.CODE_ERROR);
		}
		return res ;
	}
	/**
	 * 启用/禁用设置
	 * @param processBranchModel
	 * @return
	 */
	@RequestMapping(value="updateSet")
	@ResponseBody
	public Result updateActExProcessBranch(ActExProcessBranchModel  processBranchModel){
		Result res = new Result();
		try{
			//获取当前登录用户
			UserModel user = (UserModel) SessionMap.getObj();
			if(null == user){
				res.setCode(Result.CODE_ERROR);
				res.setMsg(ResMsg.MSG_LOGIN_NEED);
				return res;
			}
			if(StringUtil.isEmpty(processBranchModel.getStatus())){
				res.setCode(Result.CODE_ERROR);
				res.setMsg("参数出错");
				return res;
			}
			processBranchModel.setDeptId(user.getDeptParentId());
			int count = processBranchProService.updateActExProcessBranch(processBranchModel);
			if(count>0){
				res.setCode(Result.CODE_OK);
				res.setMsg("设置成功");
			}else{
				res.setCode(Result.CODE_OK);
				res.setMsg("设置失败");
			}
		}catch(Exception ex){
			log.error(ResMsg.EXECERR,ex);
			res.setMsg(ResMsg.EXECERR);
			res.setCode(Result.CODE_ERROR);
		}
		return res ;
	}
	
	@RequestMapping("getUserImgs")
	@ResponseBody
	public Result getUserImgs(OaActExUserDefinedPro userDefinedPro){
		Result res = new Result();
		try{
			//获取当前登录用户
			UserModel user = (UserModel) SessionMap.getObj();
			if(null == user){
				res.setCode(Result.CODE_ERROR);
				res.setMsg(ResMsg.MSG_LOGIN_NEED);
				return res;
			}
	
			userDefinedPro.setDeptId(user.getDeptParentId());
			//查询数据
			List<MyHashMap<String, String>> map = processBranchProService.getUserImgs(userDefinedPro);
			if(map!=null&&map.size()>0){
				res.setData(map);
				res.setCode(Result.CODE_OK);
			}else{
				res.setCode(Result.CODE_NO_DATA);
			}
		}catch(Exception ex){
			log.error("查询异常",ex);
			res.setCode(Result.CODE_ERROR);
			res.setMsg("查询异常");
		}
		return res;
	}
}
